package com.hornsun.manage.material.api;

import com.hornsun.domain.dbo.Material;
import com.hornsun.domain.dbo.Thumbnail;
import com.hornsun.manage.material.dto.MaterialDTO;
import com.hornsun.manage.material.dto.ThumbnailDTO;
import com.hornsun.repository.MaterialRepository;
import com.hornsun.repository.ThumbnailRepository;
import com.hornsun.util.UploadUtil;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.UUID;

/**
 * Created by 编译中... on 2018/5/14.
 */
@RestController
@RequestMapping("/manage/material")
public class MaterialApi {
    @Resource
    private MaterialRepository materialRepository;

    @Resource
    private ThumbnailRepository thumbnailRepository;


    /**
     * 分页获取素材
     * @param page
     * @return
     */
    @GetMapping("/getMaterial")
    public ResponseEntity<MaterialDTO> getMaterial(@RequestParam("type")int type,@RequestParam("page") int page){
        MaterialDTO materialDTO = new MaterialDTO();
        materialDTO.setMaterials(materialRepository.getMaterial(type,page));
        materialDTO.setCount(materialRepository.getCountByType(type));
        return new ResponseEntity<MaterialDTO>(materialDTO, HttpStatus.OK);
    }


    /**
     * 新增素材
     * @param material
     * @return
     */
    @Transactional
    @PostMapping("/addMaterial")
    public ResponseEntity<?> addMaterial(Material material){
        if(material==null){
            return new ResponseEntity<Object>("参数异常",HttpStatus.BAD_REQUEST);
        }
        material.setMaterialGuid(UUID.randomUUID().toString());
        materialRepository.addMaterial(material);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 修改素材
     * @param material
     * @return
     */
    @Transactional
    @PostMapping("/updateMaterial")
    public ResponseEntity<?> updateMaterial(Material material){
        material.setUpdatetime(new Date());
        materialRepository.updateMaterial(material);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     * 删除素材
     * @param materialGuid
     * @return
     */
    @Transactional
    @PostMapping("/deleteMaterial")
    public ResponseEntity<?> deleteMaterial(@RequestParam("guid")String materialGuid){
        materialRepository.deleteMaterial(materialGuid);
        return new ResponseEntity<Object>(HttpStatus.OK);
    }

    /**
     *根据guid获取素材
     * @param materialGuid
     * @return
     */
    @GetMapping("/getMaterialByGuid")
    public ResponseEntity<Material> getMaterialByGuid(@RequestParam("guid")String materialGuid){
        return new ResponseEntity<Material>(materialRepository.getMaterialByGuid(materialGuid),HttpStatus.OK);
    }

    /**
     * 上传图片到服务器
     * @param file
     * @return
     */
    @Transactional
    @PostMapping("/upload")
    public ResponseEntity<?> uploadImage(@RequestParam("file") MultipartFile file,HttpServletRequest request){
        String guid = UploadUtil.uploadImage(file);
        if (guid==null){
            return new ResponseEntity<Object>(HttpStatus.BAD_REQUEST);
        }
        Thumbnail thumbnail = new Thumbnail();
        thumbnail.setThumbnailGuid(guid);
        thumbnail.setUploadTime(new Date());
        String thumbnailName = guid+"small.jpg";
        thumbnail.setPath("http://"+request.getLocalAddr()+":"+request.getLocalPort()+"/image/"+thumbnailName);
        thumbnailRepository.insert(thumbnail);
        return new ResponseEntity<Object>(thumbnail.getPath(),HttpStatus.OK);
    }

    /**
     * 多图片上传
     * @param
     * @return
     */
   /* @PostMapping("/uploadMore")
    public ResponseEntity<?> uploadMoreImage(@RequestParam("file") MultipartFile[] files){
        if (files != null && files.length > 0) {
            for (int i = 0; i < files.length; i++) {
                Thumbnail thumbnail = UploadUtil.uploadImage(files[i]);
                thumbnailMapper.insert(thumbnail);
            }
            return new ResponseEntity<Object>(HttpStatus.OK);
        }
        return new ResponseEntity<Object>(HttpStatus.BAD_REQUEST);
    }*/

    /**
     * 获取图片素材的信息
     * @param page
     * @return
     */
   @GetMapping("/getThumbnails")
   public ResponseEntity<ThumbnailDTO> getThumbnails(@RequestParam("page")int page){
       ThumbnailDTO materialDTO = new ThumbnailDTO();
       materialDTO.setThumbnails(thumbnailRepository.getThumbnails(page));
       materialDTO.setCount(thumbnailRepository.getTotalCount());
        return new ResponseEntity<ThumbnailDTO>(materialDTO,HttpStatus.OK);
   }


    /**
     * 删除素材图片
     * @param thumbnailGuid
     * @return
     */
    @Transactional
   @PostMapping("/deleteThumbnail")
    public ResponseEntity<?> deleteThumbnail(@RequestParam("guid")String thumbnailGuid){
        thumbnailRepository.deleteByGuid(thumbnailGuid);
        return new ResponseEntity<Object>(HttpStatus.OK);
   }




}
